package cn.lbw.yht.poi.core;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import cn.lbw.yht.poi.util.AnnotationUtil;

public class ExcelHelper {

	public static void main(String[] args) {

	}


	/**
	 * 导出Excel方法
	 * @param xls List对象
	 * @param outfileName 输出的文件名带路径
	 * @param sheetName Excel页签名称
	 * @throws Exception
	 */
	public static void dto2Excel(List<?> xls,String outfileName,String sheetName) throws Exception {
		// 获取总列数
		int CountColumnNum = xls.size();
		// 创建Excel文档
		HSSFWorkbook hwb = new HSSFWorkbook();
		Object xlsDto = null;
		// sheet 对应一个工作页
		HSSFSheet sheet = hwb.createSheet(sheetName);
		HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始
		HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
		String[] names = new String[CountColumnNum];
		List<Map> colMap = AnnotationUtil.getInstance().loadVlaue(
				xls.get(0).getClass().getName());

		for (int i = 0; i < colMap.size(); i++) {
			names[i] = colMap.get(i).get("name").toString();
			HSSFFont font = (HSSFFont) hwb.createFont();
			font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 宽度
			firstcell[i] = firstrow.createCell(i);
			HSSFRichTextString ts = new HSSFRichTextString(names[i]);
			ts.applyFont(font);
			firstcell[i].setCellValue(ts);
		}

		for (int i = 0; i < xls.size(); i++) {
			// 创建一行
			HSSFRow row = sheet.createRow(i + 1);
			// 得到要插入的每一条记录
			xlsDto = xls.get(i);
			for (int colu = 0; colu < colMap.size(); colu++) {
				// 在一行内循环
				HSSFCell cell = row.createCell(colu);
				String filedName = (String) colMap.get(colu).get("filedName");
				String value = findMethodByFieldName(filedName, xlsDto.getClass()
						.getName(), xlsDto);
				cell.setCellValue(value);
			}
		}
		// 创建文件输出流，准备输出电子表格
		OutputStream out = new FileOutputStream(outfileName);
		hwb.write(out);
		out.close();
		System.out.println("数据库导出成功");
	}

	public static String findMethodByFieldName(String name, String className,
			Object obj) {
		String result = "";
		try {

			Method[] ms = Class.forName(className).getMethods();
			for (Method method : ms) {

				if (method.getName().equals(findKey(name))) {
					//如果是时间类型
					if(method.getReturnType().equals(Date.class)){
						 
						String timeFormat = AnnotationUtil.findExcelFormartByfileName(name,obj);
				        java.text.DateFormat format1 = new java.text.SimpleDateFormat(timeFormat);
						Date date = (Date)method.invoke(obj, null);
						result = format1.format(new Date());
					}else{
						result = (String) method.invoke(obj, null);
					}
					
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String findKey(String fieldName) {
		String method = "";
		method += "get";
		method += fieldName.substring(0, 1).toUpperCase();
		method += fieldName.substring(1);
		return method;
	}
}
